<!-- 2023年十月巅峰赛规则通用弹窗 -->
<!-- XXX 已改为使用RulePopup组件，本文件可考虑删除 -->
<template>
  <!-- 提示弹窗 -->
  <uni-popup ref="popup">
    <div class="content">
      <image
        class="title"
        :src="titleImg"
        mode="aspectFit"
      />
      <div class="rule-box">
        <rich-text
          class="rule-content"
          :nodes="rule"
          :selectable="false"
        />
      </div>
      <div
        class="i-know"
        @click="close"
      >
        我知道了
      </div>
    </div>
  </uni-popup>
</template>

<script setup lang="ts">
import { reqGetActivityRule } from '@/api/api-activity';
import type { ACTIVITY_RULE_KEY } from '@/static/constant/view-constant';
import { addClassToHtml } from '@/utils/tools';

type Props = {
  /** 规则Key */
  ruleKey: typeof ACTIVITY_RULE_KEY[keyof typeof ACTIVITY_RULE_KEY];
  /** 标题图片 */
  titleImg: string;
}

const props = withDefaults(defineProps<Props>(), { titleImg: '' });

const rule = ref('');

onMounted(async () => {
  if (!props.ruleKey) return;
  const ruleRes = await reqGetActivityRule(props.ruleKey);
  rule.value = addClassToHtml(ruleRes.data.data.ruleValue);
});

/** 当前组件实例 */
const currentInstance = getCurrentInstance();

/** 弹窗打开 */
const open = () => {
  console.log('open');
  const popup = currentInstance?.refs.popup as UniHelper.UniPopupInstance;
  if (popup.open) {
    popup.open();
  }
};

/** 弹窗关闭 */
const close = () => {
  const popup = currentInstance?.refs.popup as UniHelper.UniPopupInstance;
  if (popup.close) {
    popup.close();
  }
};

defineExpose({
  open,
  close,
});

</script>

<style scoped lang="scss">
.content {
  width: 92.2667vw;

  background-color: #fff;
  border-radius: 16px;

  padding: 22px 16px 24px;
  display: grid;
  grid-template:
    'title' max-content
    'rule-box' 300px
    'i-know' max-content
    / 1fr;
  row-gap: 15px;

  .title{
    grid-area: title;
    margin-bottom: 10px;

    width: 100%;
    height: 21px;
  }

  .rule-box {
    grid-area: rule-box;

    width: 100%;
    max-height: 100%;
    overflow: scroll;

    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .i-know{
    grid-area: i-know;
    justify-self: center;

    width: 64vw;
    height: 9.6vw;

    background: linear-gradient(98deg,#ff69b7, #8d70ff 100%);
    border-radius: 34px;

    display: flex;
    align-items: center;
    justify-content: center;

    font-size: 15px;
    color: #fff;
  }
}
</style>

<!-- 规则内容样式 -->
<style lang="scss">
.rich-h1 {
  font-size: 17px;
  font-weight: bold;
  color: #333;
}

.rich-p,.rich-ol {
  margin-top: 5px;

  padding: 0 0 0 20px;

  font-size: 15px;
  line-height: 28px;
  color: #333;

  .rich-li {
    margin: 5px 0;
  }
}

.rich-table {
  margin-top: 5px;

  border-collapse: collapse;

  border: 1px solid #333;

  font-size: 13px;
  color: #333;

  .rich-tr {
    border: 1px solid #333;

    .rich-td {
      border: 1px solid #333;

      padding: 5px;
    }
  }
}
</style>
